package com.idol.logger.core.printer.data

import android.os.Parcel
import android.os.Parcelable
import java.text.SimpleDateFormat
import java.util.*

internal class LoggerMo(
    private val timeMillis: Long,
    val level: Int,
    val tag: String,
    val message: String,
) : Parcelable {

    constructor(parcel: Parcel) : this(
        parcel.readLong(),
        parcel.readInt(),
        parcel.readString() ?: "",
        parcel.readString() ?: ""
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeLong(timeMillis)
        parcel.writeInt(level)
        parcel.writeString(tag)
        parcel.writeString(message)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<LoggerMo> {
        private val sdf = SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.CHINA)

        fun format(timeMillis: Long): String = sdf.format(timeMillis)

        override fun createFromParcel(parcel: Parcel): LoggerMo {
            return LoggerMo(parcel)
        }

        override fun newArray(size: Int): Array<LoggerMo?> {
            return arrayOfNulls(size)
        }
    }


    fun flattened() = "${format(timeMillis)} | $level | $tag |:"

    override fun toString(): String = "${flattened()}\n$message"

}